/*
DevBacController
Description: Controller class for DocBac page.
Version 1.0
Author: Pratibh Prakash
Created Date: 11 June-2009
*/
public class DevBacController {
	
	
	private Boolean ApexTest = false;
	private List<String> SelectedResources = new List<String>(); 
	public String ResourceName {get; set;} 
	private List<SelectOption> Options = new List<SelectOption>();
	public Boolean TokenFlag{get;set;}
	private String loginUserid;
	public String authToken{ public get { 
      	User curUser = [Select GoogleDocsToken__c from User where Id=: loginUserid];
        if(curUser.GoogleDocsToken__c==null){
        	return ApexPages.currentPage().getParameters().get('sesstoken');
        }
        else{
        	return curUser.GoogleDocsToken__c;
        }
    } private set; }
    
	public void setSelectedResources(List<String> setterList){
		this.SelectedResources.clear();
		this.SelectedResources.addAll(setterList);
	}
	
	public List<String> getSelectedResources(){
		return this.SelectedResources;
	}
	
	public List<SelectOption> getSFRes(){
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('None','-None-'));
		options.add(new SelectOption('Classes','Classes'));
		options.add(new SelectOption('Triggers','Triggers'));
		options.add(new SelectOption('VFPages','VF Pages'));
		options.add(new SelectOption('SControls','SControls'));
		return options;
	}
	
	public void checkForToken(){
		 loginUserid = UserInfo.getUserId();
		 User curUser = [Select GoogleDocsToken__c from User where Id=: loginUserid];
		 if(curUser.GoogleDocsToken__c==null){
		 	TokenFlag = true;
		 }
		 else{
		 	TokenFlag = false ;
		 }
	}
	
   	public List<SelectOption> getOptions(){
		Options.clear();
		if(ResourceName.equals('Classes')){
			ApexClass[] classes = [select Name, Id from ApexClass];
			for(ApexClass cls: classes){
				Options.add(new SelectOption(cls.Id ,cls.Name));	
			}
		}
		else if(ResourceName.equals('Triggers')){
			ApexTrigger[] triggers = [select Name, Id from ApexTrigger];
			for(ApexTrigger tgr: triggers){
				Options.add(new SelectOption(tgr.Id ,tgr.Name));	
			}
		}
		else if(ResourceName.equals('VFPages')){
			ApexPage[] pages = [select Name, Id from ApexPage];
			for(ApexPage page: pages){
				Options.add(new SelectOption(page.Id ,page.Name));	
			}
		}
		else if(ResourceName.equals('SControls')){
			Scontrol[] scontrls = [select DeveloperName, Id from Scontrol];
			for(Scontrol scs: scontrls){
				Options.add(new SelectOption(scs.Id ,scs.DeveloperName));	
			}
		}
		SelectedResources.clear();
		return Options;
	}
	
	public void Upload(){
		
		Set<String> resIds = new Set<String>();
		Integer successList=0;
		String failureList='';	
		DocumentService service = new DocumentService();
	  	service.setAuthSubToken(this.authToken);
		for(Integer i=0; i<SelectedResources.size(); i++){
			resIds.add(SelectedResources.get(i));
		}
		if(resIds.size()>0){
			if(ResourceName.equals('Classes')){
				ApexClass[] classes = [select Name,Body from ApexClass where Id in: resIds];
				try{
					for(Integer j=0; j<classes.size(); j++ ){
						GoogleData doc;
						if(!ApexTest){
							doc = service.uploadDocument(classes[j].Body,classes[j].Name,'text/plain');
						} 
						if(doc!=null){
							doc.dump();
							if(doc.feed!= null){   
								successList +=1;
							}  
							else{
								failureList +=classes[j].Name+',';
							}
						}
					}
					if(failureList.length()>0){
						ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, String.valueOf(successList)+'  Resource Successfully Uploaded to Google Documents.'+failureList+'\n FAILED TO UPLOAD');
		       			ApexPages.addMessage(myMsg);
					}
					else{
						ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, String.valueOf(successList)+'  Resource Successfully Uploaded to Google Documents.');
		       			ApexPages.addMessage(myMsg);
					}
				}
				catch(Exception ex){
					ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, String.valueOf(successList)+' Resource Successfully Uploaded   '+(classes.size()-successList)+'Failed to Upload.  '+ex.getMessage());
		       		ApexPages.addMessage(myMsg);
				}
			}
			else if(ResourceName.equals('Triggers')){
				ApexTrigger[] triggers = [select Name, Body from ApexTrigger where Id in: resIds];	
				try{
					for(Integer j=0; j<triggers.size(); j++ ){
						GoogleData doc;
						if(!ApexTest){
							doc = service.uploadDocument(triggers[j].Body,triggers[j].Name,'text/plain');
						} 
						if(doc!=null){
							doc.dump();   
							if(doc.feed!= null){   
								successList +=1;
							}  
							else{
								failureList +=triggers[j].Name+'\n';
							}
						}
					}
					if(failureList.length()>0){
						ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, String.valueOf(successList)+'  Resource Successfully Uploaded to Google Documents.'+failureList+'\n FAILED TO UPLOAD');
		       			ApexPages.addMessage(myMsg);
					}
					else{
						ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, String.valueOf(successList)+'  Resource Successfully Uploaded to Google Documents.');
		       			ApexPages.addMessage(myMsg);
					}
				}
				catch(Exception ex){
					ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, String.valueOf(successList)+'  Resource Successfully Uploaded   '+(triggers.size()-successList)+'Failed to Upload.   '+ex.getMessage());
		       		ApexPages.addMessage(myMsg);
				}
			}
			else if(ResourceName.equals('VFPages')){
				ApexPage[] pages = [select Name, Markup from ApexPage where Id in: resIds];	
				try{
					for(Integer j=0; j<pages.size(); j++ ){
						GoogleData doc;
						if(!ApexTest){
							doc = service.uploadDocument(pages[j].Markup,pages[j].Name,'text/plain');
						} 
						if(doc!=null){
							doc.dump();   
							if(doc.feed!= null){   
								successList +=1;
							}  
							else{
								failureList +=pages[j].Name+'\n';
							}
						}
					}
					if(failureList.length()>0){
						ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, String.valueOf(successList)+'  Resource Successfully Uploaded to Google Documents.'+failureList+'\n FAILED TO UPLOAD');
		       			ApexPages.addMessage(myMsg);
					}
					else{
						ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, String.valueOf(successList)+'  Resource Successfully Uploaded to Google Documents.');
		       			ApexPages.addMessage(myMsg);
					}
				}
				catch(Exception ex){
					ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, String.valueOf(successList)+'  Resource Successfully Uploaded   '+(pages.size()-successList)+'Failed to Upload.   '+ex.getMessage());
		       		ApexPages.addMessage(myMsg);
				}
			}
			else if(ResourceName.equals('SControls')){
				Scontrol[] scntrls = [select DeveloperName, HtmlWrapper from Scontrol where Id in: resIds];	
				try{
					for(Integer j=0; j<scntrls.size(); j++ ){
						GoogleData doc;
						if(!ApexTest){
							doc = service.uploadDocument(scntrls[j].HtmlWrapper,scntrls[j].DeveloperName,'text/plain');
						}
						if(doc!=null){ 
							doc.dump();   
							if(doc.feed!= null){   
								successList +=1;
							}  
							else{
								failureList +=scntrls[j].DeveloperName+'\n';
							}
						}
					}
					
					if(failureList.length()>0){
						ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, String.valueOf(successList)+'  Resource Successfully Uploaded to Google Documents.'+failureList+'\n FAILED TO UPLOAD');
		       			ApexPages.addMessage(myMsg);
					}
					else{
						ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, String.valueOf(successList)+'  Resource Successfully Uploaded to Google Documents.');
		       			ApexPages.addMessage(myMsg);
					}
				}
				catch(Exception ex){
					ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, String.valueOf(successList)+'  Resource Successfully Uploaded   '+(scntrls.size()-successList)+'Failed to Upload.   '+ex.getMessage());
		       		ApexPages.addMessage(myMsg);
				}
			}
		}
		else{
			ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, 'Please Select a Resource');
	       	ApexPages.addMessage(myMsg);
		}
		if(TokenFlag){
			User curUser = [Select GoogleDocsToken__c from User where Id=: loginUserid];
        	curUser.GoogleDocsToken__c =this.authToken;
        	update curUser;
		}
	}
	
	public void InitApexTest(){
		ApexTest = true;
	}
}